package com.ocr.utils;

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Bitmap.Config;
import android.util.Log;

import com.jabistudio.androidjhlabs.filter.ContrastFilter;
import com.jabistudio.androidjhlabs.filter.DoGFilter;
import com.jabistudio.androidjhlabs.filter.ReduceNoiseFilter;
import com.jabistudio.androidjhlabs.filter.util.AndroidUtils;

public class ProcessImgUtil {
	private static final int MAX_SIZE_UN_PROCESS = 1228800;

	private int getHistoramCase(Bitmap bitmap) {
		long result = 0;
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		Log.d(ApplicationConstant.LOGT_TAG, "img size:"+(width*height));
		if (width * height > MAX_SIZE_UN_PROCESS)
			return 3;// img too big ---> not process
		int r, g, b;
		int count = 0;
		for (int i = 0; i < width; i++) {
			for (int j = 0; j < height; j++) {
				int pixel = bitmap.getPixel(i, j);
				r = Color.red(pixel);
				g = Color.green(pixel);
				b = Color.blue(pixel);
				float y = 0.2126f * r + 0.7152f * g + 0.0722f * b;
				result += y;
				count++;
				// Log.d("------------thoainguyen caculateB result:",
				// String.valueOf(result));
				// Log.d("------------thoainguyen caculateB count:",
				// String.valueOf(count));
			}
		}
		float kq = result / count;
		Log.d(ApplicationConstant.LOGT_TAG, "historam:"+kq);
		if (kq < 50)           
			return -1;// too dark to process
		if (kq > 200)
			return 2;// ok or too light
		return 1;// normal --> process
	}

	public Bitmap processImg(Bitmap bitmap) {
		int imgHistoramCase = getHistoramCase(bitmap);
		Log.d(ApplicationConstant.LOGT_TAG, "imgHistoramCase:"+imgHistoramCase);
		if (imgHistoramCase == 1) {
			Log.d(ApplicationConstant.LOGT_TAG, "processing img");
			try {
				bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
				int width = bitmap.getWidth();
				int height = bitmap.getHeight();
				int[] src;
				int[] result;
				/*
				 * remove noise
				 */
				ReduceNoiseFilter reduceNoiseFilter = new ReduceNoiseFilter();
				src = AndroidUtils.bitmapToIntArray(bitmap);
				result = reduceNoiseFilter.filter(src, width, height);
				bitmap = Bitmap.createBitmap(result, width, height,
						Config.ARGB_8888);
				/*
				 * adjust if too dark
				 */
				// increase brightness and reduce contrast
				ContrastFilter filter = new ContrastFilter();
				filter.setContrast(0.8f);
				filter.setBrightness(1.2f);
				// Change int Array into a bitmap
				src = AndroidUtils.bitmapToIntArray(bitmap);
				// Applies a filter.
				result = filter.filter(src, width, height);
				// Change the Bitmap int Array (Supports only ARGB_8888)
				bitmap = Bitmap.createBitmap(result, width, height,
						Config.ARGB_8888);
				// dog fillter
				src = AndroidUtils.bitmapToIntArray(bitmap);
				DoGFilter doGFilter = new DoGFilter();
//				 doGFilter.setRadius1(doGFilter.getRadius1()-5);
//				 doGFilter.setRadius2(doGFilter.getRadius2()-5);
				doGFilter.setNormalize(true);
				doGFilter.setInvert(true);
				result = doGFilter.filter(src, width, height);
				bitmap = Bitmap.createBitmap(result, width, height,
						Config.ARGB_8888);
				return bitmap;
			} catch (Exception ex) {
				return bitmap;
			}
		}
		return bitmap;

	}
}
